<template>
  <div class="product-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">商城列表</h1>
      <div class="header-actions">
        <div class="search-box">
          <input type="text" placeholder="搜索商品名称" class="search-input" v-model="searchText" />
          <button class="search-btn" @click="searchProducts">搜索</button>
        </div>
      </div>
    </div>

    <!-- 添加商品区域 -->
    <div class="add-section">
      <div class="add-box">
        <input type="text" placeholder="请输入商品名称" class="add-input" v-model="str" />
        <input type="text" placeholder="请输入商品价格" class="add-input" v-model="jg" />
        <input type="text" placeholder="请输入商品描述" class="add-input" v-model="desc" />
        <input type="url" placeholder="请输入商品图片" class="add-input" v-model="tp" />
        <button class="add-btn" @click="add">
          <span class="btn-icon">+</span>
          添加商品
        </button>
      </div>
    </div>

    <!-- 商品列表 -->
    <div class="product-list">
      <div class="list-content">
        <div v-for="item in list" :key="item.proid" class="list-item">
          <div class="item-cell">
            <img :src="item.img1" :alt="item.proname" class="product-image" />
          </div>
          <div class="item-cell">
            <span class="product-name">{{ item.proname }}</span>
          </div>
          <div class="item-cell">
            <span class="product-price">¥{{ item.originprice }}</span>
          </div>
          <!-- <div class="item-cell">
            <span class="product-name">{{ item.desc }}</span>
          </div> -->
          <div class="item-cell actions">
            <button class="delete-btn" @click="deleteProduct(item.proid)">删除</button>
            <button class="edit-btn" @click="editProduct(item.proid)">修改</button>
          </div>
        </div>
      </div>
    </div>
    <!-- 编辑商品弹框 -->
    <div v-if="flag" class="modal-overlay" @click="closeModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>编辑商品</h3>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label>商品名称</label>
            <input type="text" placeholder="请输入商品名称" class="modal-input" v-model="mc" />
          </div>
          <div class="form-group">
            <label>商品价格</label>
            <input type="text" placeholder="请输入商品价格" class="modal-input" v-model="dsq" />
          </div>
          <div class="form-group">
            <label>商品描述</label>
            <input type="text" placeholder="请输入商品描述" class="modal-input" v-model="desc" />
          </div>
          <div class="form-group">
            <label>商品图片</label>
            <input type="url" placeholder="请输入商品图片" class="modal-input" v-model="zp" />
          </div>
        </div>
        <div class="modal-footer">
          <button class="save-btn" @click="saveEdit">保存</button>
          <button class="cancel-btn" @click="closeModal">取消</button>
        </div>
      </div>
    </div>
    <!-- 分页组件 -->
    <div class="pagination">
      <button class="page-btn" :disabled="currentPage === 1" @click="changePage(currentPage - 1)">
        上一页
      </button>

      <div class="page-numbers">
        <button
          v-for="page in visiblePages"
          :key="page"
          class="page-number"
          :class="{ active: page === currentPage }"
          @click="changePage(page)"
        >
          {{ page }}
        </button>
      </div>

      <button
        class="page-btn"
        :disabled="currentPage === totalPages"
        @click="changePage(currentPage + 1)"
      >
        下一页
      </button>

      <div class="page-info">
        第 {{ currentPage }} 页，共 {{ totalPages }} 页，共 {{ totalCount }} 条记录
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      list: [],
      searchText: '',
      currentPage: 1,
      pageSize: 12,
      totalCount: 0,
      totalPages: 0,
      str: '',
      jg: '',
      tp: '',
      flag: false,
      mc: '',
      dsq: '',
      zp: '',
      id: null,
      desc: '',
    }
  },
  computed: {
    visiblePages() {
      const pages = []
      const start = Math.max(1, this.currentPage - 2)
      const end = Math.min(this.totalPages, this.currentPage + 2)

      for (let i = start; i <= end; i++) {
        pages.push(i)
      }
      return pages
    },
  },
  methods: {
    async loadProducts() {
      try {
        const params = {
          count: this.currentPage,
          limitNum: this.pageSize,
        }

        if (this.searchText.trim()) {
          params.search = this.searchText.trim()
        }

        const response = await axios.get('http://47.94.6.186:3001/admin/pro/list', {
          headers: {
            token: localStorage.getItem('loginToken'),
          },
          params: params,
        })

        console.log(response.data)
        this.list = response.data.data || []
        this.totalCount = response.data.total || 0
        this.totalPages = Math.ceil(this.totalCount / this.pageSize)
      } catch (error) {
        console.error('加载商品失败:', error)
        this.list = []
      }
    },

    changePage(page) {
      if (page >= 1 && page <= this.totalPages && page !== this.currentPage) {
        this.currentPage = page
        this.loadProducts()
      }
    },

    searchProducts() {
      this.currentPage = 1
      this.loadProducts()
    },
    deleteProduct(id) {
      axios
        .delete(`http://47.94.6.186:3001/admin/pro/product/${id}`, {
          headers: {
            token: localStorage.getItem('loginToken'),
          },
        })
        .then((res) => {
          console.log(res)
          this.loadProducts()
        })
    },
    add() {
      axios
        .post(
          'http://47.94.6.186:3001/admin/pro/product',
          {
            proname: this.str,
            originprice: this.jg,
            img1: this.tp,
            desc: this.desc,
          },
          {
            headers: {
              token: localStorage.getItem('loginToken'),
            },
          },
        )
        .then((res) => {
          console.log(res)
          this.loadProducts()
          this.jg = ''
          this.str = ''
          this.tp = ''
          this.desc = ''
        })
    },
    editProduct(id) {
      this.flag = true
      this.proid = id
      this.mc = this.list.find((item) => item.proid === id).proname
      this.dsq = this.list.find((item) => item.proid === id).originprice
      this.zp = this.list.find((item) => item.proid === id).img1
      this.desc = this.list.find((item) => item.proid === id).desc
    },
    closeModal() {
      this.flag = false
      this.mc = ''
      this.dsq = ''
      this.zp = ''
      this.desc = ''
      this.proid = null
    },
    saveEdit() {
      axios
        .patch(
          `http://47.94.6.186:3001/admin/pro/product/${this.proid}`,
          {
            proname: this.mc,
            originprice: this.dsq,
            img1: this.zp,
            desc: this.desc,
          },
          {
            headers: {
              token: localStorage.getItem('loginToken'),
            },
          },
        )
        .then((res) => {
          console.log('修改成功:', res.data)
          alert('商品修改成功！')
          this.loadProducts() // 刷新页面数据
          this.closeModal() // 关闭弹框
        })
        .catch((error) => {
          console.error('修改失败:', error)
          alert('修改失败，请重试！')
        })
    },
  },
  mounted() {
    this.loadProducts()
  },
}
</script>

<style scoped>
.product-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.page-header {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.page-title {
  font-size: 24px;
  color: #333;
  margin: 0 0 20px 0;
  font-weight: 600;
}

.header-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-box {
  display: flex;
  gap: 15px;
  align-items: center;
}

.search-input {
  padding: 10px 15px;
  border: 2px solid #e1e5e9;
  border-radius: 6px;
  font-size: 14px;
  width: 300px;
  outline: none;
  transition: border-color 0.3s;
}

.search-input:focus {
  border-color: #409eff;
}

.search-btn {
  background: linear-gradient(135deg, #409eff, #66b1ff);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s;
}

.search-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.add-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.add-box {
  display: flex;
  gap: 15px;
  align-items: center;
  justify-content: center;
}

.add-input {
  padding: 10px 15px;
  border: 2px solid #e1e5e9;
  border-radius: 6px;
  font-size: 14px;
  width: 300px;
  outline: none;
  transition: border-color 0.3s;
}

.add-input:focus {
  border-color: #4caf50;
}

.add-btn {
  background: linear-gradient(135deg, #4caf50, #45a049);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s;
}

.add-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
}

.product-list {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  margin-bottom: 20px;
}

.list-content {
  max-height: 600px;
  overflow-y: auto;
}

.list-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #f0f0f0;
  transition: background-color 0.3s;
  cursor: pointer;
}

.list-item:hover {
  background-color: #f8f9fa;
}

.item-cell {
  display: flex;
  align-items: center;
}

.product-image {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 6px;
  margin-right: 15px;
}

.product-name {
  font-weight: 500;
  color: #333;
  flex: 1;
  margin-right: 20px;
}

.product-price {
  font-size: 18px;
  font-weight: bold;
  color: #e74c3c;
  min-width: 100px;
  text-align: right;
}

.actions {
  gap: 10px;
}

.edit-btn,
.delete-btn {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.3s;
}

.edit-btn {
  background-color: #e3f2fd;
  color: #1976d2;
  margin-right: 8px;
}

.edit-btn:hover {
  background-color: #1976d2;
  color: white;
}

.delete-btn {
  background-color: #ffebee;
  color: #d32f2f;
}

.delete-btn:hover {
  background-color: #d32f2f;
  color: white;
}

.pagination {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
  flex-wrap: wrap;
}

.page-btn {
  padding: 8px 16px;
  border: 1px solid #dcdfe6;
  background: white;
  color: #606266;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.page-btn:hover:not(:disabled) {
  background: #409eff;
  color: white;
  border-color: #409eff;
}

.page-btn:disabled {
  background: #f5f7fa;
  color: #c0c4cc;
  cursor: not-allowed;
}

.page-numbers {
  display: flex;
  gap: 5px;
}

.page-number {
  width: 32px;
  height: 32px;
  border: 1px solid #dcdfe6;
  background: white;
  color: #606266;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-number:hover {
  background: #409eff;
  color: white;
  border-color: #409eff;
}

.page-number.active {
  background: #409eff;
  color: white;
  border-color: #409eff;
}

.page-info {
  color: #909399;
  font-size: 14px;
  margin-left: 15px;
}

.btn-icon {
  font-size: 14px;
}

/* 弹框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  width: 500px;
  max-width: 90vw;
  max-height: 90vh;
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #e1e5e9;
  background-color: #f8f9fa;
}

.modal-header h3 {
  margin: 0;
  color: #333;
  font-size: 18px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: #999;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s;
}

.close-btn:hover {
  background-color: #f0f0f0;
  color: #666;
}

.modal-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

.modal-input {
  width: 100%;
  padding: 12px 15px;
  border: 2px solid #e1e5e9;
  border-radius: 6px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s;
  box-sizing: border-box;
}

.modal-input:focus {
  border-color: #409eff;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 20px;
  border-top: 1px solid #e1e5e9;
  background-color: #f8f9fa;
}

.save-btn {
  background: linear-gradient(135deg, #4caf50, #45a049);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.save-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
}

.cancel-btn {
  background: #f5f7fa;
  color: #606266;
  border: 1px solid #dcdfe6;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.cancel-btn:hover {
  background: #e1e5e9;
  border-color: #c0c4cc;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-box {
    flex-direction: column;
    align-items: stretch;
  }

  .search-input {
    width: 100%;
  }

  .list-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .product-image {
    margin-right: 0;
  }

  .pagination {
    flex-direction: column;
    gap: 10px;
  }

  .page-info {
    margin-left: 0;
    text-align: center;
  }
}

@media (max-width: 480px) {
  .page-numbers {
    flex-wrap: wrap;
    justify-content: center;
  }
}
</style>
